package facelets.component;


import javax.el.ValueExpression;
import javax.faces.component.UIComponent;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.tag.MetaRuleset;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.jsf.ComponentConfig;
import com.sun.facelets.tag.jsf.html.HtmlComponentHandler;

public class ConfirmCommandComponentHandler extends HtmlComponentHandler { // NO_UCD

	private final TagAttribute message;
	
	public ConfirmCommandComponentHandler(ComponentConfig tagConfig) {
		super(tagConfig);
		message = super.getRequiredAttribute("confirmMessage");
	}
	
	/* (non-Javadoc)
	 * @see facelets.component.CommandComponentHandler#setAttributes(com.sun.facelets.FaceletContext, java.lang.Object)
	 */
	@Override
	public void setAttributes(FaceletContext ctx, Object instance) {
		super.setAttributes(ctx, instance);
		String s = "if (!confirm('" + message.getValue() + "')) return false";
		UIComponent c = (UIComponent)instance;
		ValueExpression vel = ctx.getExpressionFactory().createValueExpression(ctx, s, String.class);
		
		c.setValueExpression("onclick", vel);
	}
	
	protected MetaRuleset createMetaRuleset(@SuppressWarnings("rawtypes") Class type) {
		return super.createMetaRuleset(type).ignore("confirmMessage");
    }
	

}
